package com.itheima.controller;


import com.itheima.health.resource.aliyunmessage.SMSUtils;
import com.itheima.health.resource.code.ValidateCodeUtils;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisMessageConstant;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.SetmealService;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("member")
public class MemberControler {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private MemberService memberService;

    //登录页面验证码
    @RequestMapping("sendTelephone.do")
    public Result sendTelephone(@RequestParam("telephone") String telephone){
        //发送验证码到指定的手机号，并且将发送的验证码保存起来，并且设置验证码保存的时间
        //保存到redis缓存中，redis还可以设置保存数据的生存周期
        try {

            //生成一个随机的验证码
            String randomcode = ValidateCodeUtils.generateValidateCode(6);
            System.out.println(randomcode);
            //SMSUtils.validaCodeSms(telephone, randomcode);

            //redis保存验证码30秒,这里调用的方法值是毫秒值
            redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_LOGIN+telephone,randomcode,30000);

            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }


    //登录页面信息验证进行登录
    @RequestMapping("login.do")
    public Result login(@RequestParam("telephone") String telephone,@RequestParam("code") String code){
        try {
            //1.先是获取redis中保存的验证码，拿出来与用户在前端页面输入的进行对比，判断是否正确
            if (code!=null){
                //以redis中的key值来获取redis中存储的code
                String redisCode =(String) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_LOGIN + telephone);
                //但是也有可能为null值，因为用户输入验证码的时间过了设定的验证码保留时间，redis会自动删掉
                if (redisCode==null){
                    //验证码过期
                    return new Result(false,MessageConstant.VALIDATECODE_TIMEOUT);
                }else{
                    //不过期  和用户输入的去比对
                    if (redisCode.equals(code)){
                        //先创建方法去查找，判断是否是新用户,新用户就去添加
                        Member member = memberService.findByTelephone(telephone);
                        if (member==null){
                            member = new Member();
                            member.setPhoneNumber(telephone);
                            member.setRegTime(new Date());
                            memberService.save(member);
                        }
                        return new Result(true);
                    }else {
                        return new Result(false,MessageConstant.VALIDATECODE_ERROR);
                    }
                }

            }else {
                return new Result(false,"验证码非法");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }




    //预约验证码
    @RequestMapping("send2Telephone.do")
    public Result send2Telephone(@RequestParam("telephone") String telephone){
    //发送验证码到指定的手机号，并且将发送的验证码保存起来，并且设置验证码保存的时间
        //保存到redis缓存中，redis还可以设置保存数据的生存周期
        try {

            //生成一个随机的验证码
            String randomcode = ValidateCodeUtils.generateValidateCode(6);
            System.out.println(randomcode);
            //SMSUtils.validaCodeSms(telephone, randomcode);

            //redis保存验证码30秒,这里调用的方法值是毫秒值
            redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER+telephone,randomcode,30000);

            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }


}
